package com.jk.garden1.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jk.garden1.dao.SystemLogDao;
import com.jk.garden1.dao.UserMinDao;
import com.jk.garden1.dao.UserTableDao;
import com.jk.garden1.entity.SystemLogEntity;
import com.jk.garden1.entity.UserMiniEntity;
import com.jk.garden1.entity.UserTableEntity;
import com.jk.garden1.service.UserService;
import com.jk.garden1.util.Md5UUIDSaltUtil;
import com.jk.garden1.util.MsgUtil;
import com.jk.garden1.util.SendCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceimpl extends ServiceImpl<UserTableDao, UserTableEntity> implements UserService {

    @Autowired
    private UserTableDao userTableDao;

    @Autowired
    private SystemLogDao systemLogDao;

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private UserMinDao userMinDao;

    @Override
    public UserTableEntity findByUsername(String username) {

        QueryWrapper<UserTableEntity> QueryWrapper=new QueryWrapper<>();
        QueryWrapper.eq("username",username);
        return userTableDao.selectOne(QueryWrapper);

    }

    //注册验证码
    @Override
    public Map SendCode(String phone) {
        String key="code"+phone;

        JSONObject obj = SendCode.sendCode(phone);
        int code = obj.getIntValue("code");

        if(code!=200){
//            //获取发送的验证码
//            String round = obj.getString("obj");
//            //存入Redis,设置60秒过期
//            redisTemplate.opsForValue().set(key,round,3000,TimeUnit.SECONDS);
            return MsgUtil.returnMsg(251,"发送失败",null);
        }
        //获取发送的验证码
        String round = obj.getString("obj");
        System.out.println(round);
        //存入Redis,设置60秒过期
        redisTemplate.opsForValue().set(key,round,3000,TimeUnit.SECONDS);
        return MsgUtil.returnMsg(200,"发送成功,请注意查收",null);
    }


    @Override
    public Map registerUser(UserTableEntity usertableEntity) {
        String key="code"+usertableEntity.getPhone();
        if(!redisTemplate.hasKey(key)){
            return MsgUtil.returnMsg(253,"验证码已过期",null);
        }
        String code2 = (String) redisTemplate.opsForValue().get(key);
        if(!code2.equals(usertableEntity.getCode())){
            return MsgUtil.returnMsg(252,"验证码错误，请重新获取",null);
        }else{
            //加盐,并将加过盐的密码 和盐重新存入数据库中
            String salt = Md5UUIDSaltUtil.getSalt();
            //自己想搞什么搞什么 加密
            String mySalt=salt.substring(0,5);
            SimpleDateFormat simpleDateFormat;
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String str = simpleDateFormat.format(date);
            String password = Md5UUIDSaltUtil.createMd5Code(usertableEntity.getPassword()+mySalt);
            usertableEntity.setPassword(password);
            usertableEntity.setSalt(salt);
            usertableEntity.setRole(usertableEntity.getRole());
            usertableEntity.setStatus("正常");
            usertableEntity.setUsername(usertableEntity.getUsername());
            usertableEntity.setCreatetime(str);
            userTableDao.insert(usertableEntity);

            SystemLogEntity systemLogEntity = new SystemLogEntity();
            systemLogEntity.setOnefunction("系统日志");
            systemLogEntity.setTwofunction("用户管理");
            systemLogEntity.setLogcontent("注册新用户");
            systemLogEntity.setCreateperson(usertableEntity.getName());
            systemLogEntity.setCreatetime(str);
            systemLogDao.insert(systemLogEntity);
        }
        // redisTemplate.delete(key);
        QueryWrapper<UserTableEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",usertableEntity.getPhone());
        UserTableEntity user = userTableDao.selectOne(wrapper);


        String token = UUID.randomUUID().toString().replaceAll("-", "");
        redisTemplate.opsForValue().set(token, usertableEntity.getPhone(), 3600, TimeUnit.SECONDS);
        user.setToken(token);
        user.setPassword(usertableEntity.getPassword());
        return MsgUtil.returnMsg(200,"注册成功",user);
    }

    //手机号登录
    @Override
    public UserTableEntity findByUser(String phone) {
        QueryWrapper<UserTableEntity> QueryWrapper=new QueryWrapper<>();
        QueryWrapper.eq("phone",phone);
        return userTableDao.selectOne(QueryWrapper);
    }

    //查询
    @Override
    public UserTableEntity selelist(Integer id) {
        QueryWrapper<UserTableEntity> QueryWrapper=new QueryWrapper<>();
        QueryWrapper.eq("id",id);
        return userTableDao.selectOne(QueryWrapper);
    }

    //修改密码
    @Override
    public void getpwdupdate(UserTableEntity userEntity) {
        //加盐,并将加过盐的密码 和盐重新存入数据库中
        String salt = Md5UUIDSaltUtil.getSalt();
        //自己想搞什么搞什么 加密
        String mySalt=salt.substring(0,5);

        String password = Md5UUIDSaltUtil.createMd5Code(userEntity.getPassword()+mySalt);
        userEntity.setPassword(password);
        userEntity.setSalt(salt);
        userTableDao.updateById(userEntity);
    }



    /**
     * 验证码登录
     * @param userMiniEntity
     * @return
     */
    @Override
    public Object  loginCode(UserMiniEntity userMiniEntity) {
        UserMiniEntity userMiniEntity1 = userMinDao.queryPhone(userMiniEntity.getPhone());
        if (userMiniEntity1 == null) {
            userMiniEntity.setId(UUID.randomUUID().toString());
            userMinDao.insert(userMiniEntity);
        }
            QueryWrapper<UserMiniEntity> QueryWrapper=new QueryWrapper<>();
            QueryWrapper.eq("phone",userMiniEntity.getPhone());
            return userMinDao.selectOne(QueryWrapper);


    }
}
